package nc.impl.hi.psndoc;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import nc.bs.dao.BaseDAO;
import nc.bs.dao.DAOException;
import nc.bs.framework.common.InvocationInfoProxy;
import nc.bs.framework.common.NCLocator;
import nc.bs.framework.core.util.ObjectCreator;
import nc.hr.frame.persistence.SimpleDocServiceTemplate;
import nc.hr.utils.InSQLCreator;
import nc.hr.utils.PubEnv;
import nc.hr.utils.ResHelper;
import nc.itf.hi.IPersonRecordService;
import nc.itf.hr.frame.IPersistenceRetrieve;
import nc.itf.om.IAOSQueryService;
import nc.md.persist.framework.IMDPersistenceQueryService;
import nc.md.persist.framework.IMDPersistenceService;
import nc.md.persist.framework.MDPersistenceService;
import nc.pub.tools.HiSQLHelper;
import nc.pub.tools.VOUtils;
import nc.vo.bd.psn.PsndocExtend;
import nc.vo.bd.psn.PsnjobVO;
import nc.vo.hi.psndoc.CertVO;
import nc.vo.hi.psndoc.PartTimeVO;
import nc.vo.hi.psndoc.PsnChgVO;
import nc.vo.hi.psndoc.PsnJobVO;
import nc.vo.hi.psndoc.PsnOrgVO;
import nc.vo.hi.psndoc.PsndocAggVO;
import nc.vo.hi.pub.HiEventValueObject;
import nc.vo.hr.infoset.DefineMap;
import nc.vo.hrcm.make.ConttypeStateVO;
import nc.vo.org.JobVO;
import nc.vo.org.OrgVO;
import nc.vo.org.PostVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.CircularlyAccessibleValueObject;
import nc.vo.pub.SuperVO;
import nc.vo.pub.lang.UFBoolean;
import nc.vo.pub.lang.UFLiteralDate;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;

















public class PsndocUAPToHRServiceImpl
  implements nc.itf.bd.psn.psndoc.IPsndocService
{
  public static final String uapMoudle = "baseapp";
  public static final String uapImplClass = "nc.impl.bd.psn.psndoc.PsndocServiceImpl";
  private SimpleDocServiceTemplate serviceTemplate;
  private BaseDAO baseDAO;
  
  public PsndocUAPToHRServiceImpl() {}
  
  public nc.vo.bd.psn.PsndocVO insertPsndoc(nc.vo.bd.psn.PsndocVO vo, boolean isCheckRepeat)
    throws BusinessException
  {
    nc.itf.bd.psn.psndoc.IPsndocService service = (nc.itf.bd.psn.psndoc.IPsndocService)ObjectCreator.newInstance("baseapp", "nc.impl.bd.psn.psndoc.PsndocServiceImpl");
    if (!PubEnv.isModuleStarted(null, "6007"))
    {
      return service.insertPsndoc(vo, isCheckRepeat);
    }
    nc.vo.hi.psndoc.PsndocVO doc = (nc.vo.hi.psndoc.PsndocVO)nc.vo.om.aos.SuperVOHelper.createSuperVOFromSuperVO(vo, nc.vo.hi.psndoc.PsndocVO.class);
    PsndocAggVO agg = ((nc.itf.hi.IPsndocService)NCLocator.getInstance().lookup(nc.itf.hi.IPsndocService.class)).checkPsnUnique(doc, false);
    if ((agg != null) && (!agg.getParentVO().getIsuapmanage().booleanValue()) && (agg.getParentVO().getPsnOrgVO() != null) && (agg.getParentVO().getPsnOrgVO().getEndflag() != null) && (!agg.getParentVO().getPsnOrgVO().getEndflag().booleanValue()))
    {

      throw new BusinessException(ResHelper.getString("6007psn", "06007psn0479"));
    }
    

    nc.vo.bd.psn.PsndocVO uapPsnVO = service.insertPsndoc(vo, isCheckRepeat);
    

    if ((uapPsnVO.getDataoriginflag() != null) && ((uapPsnVO.getDataoriginflag().intValue() == 1) || (uapPsnVO.getDataoriginflag().intValue() == 2)))
    {

      return uapPsnVO;
    }
    

    doInsertHRPsndoc(uapPsnVO, true, 1);
    
    return (nc.vo.bd.psn.PsndocVO)MDPersistenceService.lookupPersistenceQueryService().queryBillOfVOByPK(nc.vo.bd.psn.PsndocVO.class, uapPsnVO.getPk_psndoc(), false);
  }
  












  private void doInsertHRPsndoc(nc.vo.bd.psn.PsndocVO uapPsnVO, boolean isSyncCert, int orgrelaid)
    throws BusinessException
  {
    PsnjobVO[] uapJobVO = uapPsnVO.getPsnjobs();
    
    if ((uapJobVO == null) || (uapJobVO.length == 0))
    {
      return;
    }
    

    syncDefineAtrrs(uapPsnVO);
    
    if (isSyncCert)
    {

      syncCertVO(uapPsnVO);
    }
    


    PsnOrgVO orgVO = SyncCreateOrg(uapJobVO, orgrelaid);
    

    List<PsnJobVO> jobList = syncHRPsnjobs(orgVO, uapJobVO);
    
    PsnJobVO mainJobVO = null;
    
    for (PsnJobVO psnJobVO : jobList)
    {
      if ((psnJobVO.getIsmainjob() != null) && (psnJobVO.getIsmainjob().booleanValue()))
      {


        mainJobVO = psnJobVO;
        ((IPersonRecordService)NCLocator.getInstance().lookup(IPersonRecordService.class)).addPsnChgWhenIntoDoc(psnJobVO);
      }
    }
    

    if (PubEnv.isModuleStarted(PubEnv.getPk_group(), "6011"))
    {

      ConttypeStateVO stateVO = new ConttypeStateVO();
      stateVO.setAssgid(mainJobVO.getAssgid());
      stateVO.setContstate(Integer.valueOf(1));
      stateVO.setConttype("hi_psndoc_ctrt");
      stateVO.setPk_psndoc(uapPsnVO.getPk_psndoc());
      stateVO.setPk_psnorg(orgVO.getPk_psnorg());
      getServiceTemplate().insert(stateVO);
    }
  }
  

  public nc.vo.bd.psn.PsndocVO updatePsndoc(nc.vo.bd.psn.PsndocVO vo, boolean isCheckRepeat)
    throws BusinessException
  {
    nc.itf.bd.psn.psndoc.IPsndocService service = (nc.itf.bd.psn.psndoc.IPsndocService)ObjectCreator.newInstance("baseapp", "nc.impl.bd.psn.psndoc.PsndocServiceImpl");
    if (!PubEnv.isModuleStarted(null, "6007"))
    {
      return service.updatePsndoc(vo, isCheckRepeat);
    }
    

    nc.vo.hi.psndoc.PsndocVO doc = (nc.vo.hi.psndoc.PsndocVO)nc.vo.om.aos.SuperVOHelper.createSuperVOFromSuperVO(vo, nc.vo.hi.psndoc.PsndocVO.class);
    
    PsndocAggVO agg = ((nc.itf.hi.IPsndocService)NCLocator.getInstance().lookup(nc.itf.hi.IPsndocService.class)).checkPsnUnique(doc, false);
    



    nc.vo.bd.psn.PsndocVO uapPsnVO = service.updatePsndoc(vo, isCheckRepeat);
    
    if ((uapPsnVO.getDataoriginflag() != null) && ((uapPsnVO.getDataoriginflag().intValue() == 1) || (uapPsnVO.getDataoriginflag().intValue() == 2)))
    {

      return uapPsnVO;
    }
    

    if ((agg != null) && (!agg.getParentVO().getIsuapmanage().booleanValue()) && (agg.getParentVO().getPsnOrgVO().getEndflag().booleanValue()))
    {


      doInsertHRPsndocWhenRehire(uapPsnVO, false, agg.getParentVO().getPsnOrgVO().getOrgrelaid().intValue() + 1);

    }
    else
    {
      doUpdateHRPsndoc(uapPsnVO);
    }
    return (nc.vo.bd.psn.PsndocVO)MDPersistenceService.lookupPersistenceQueryService().queryBillOfVOByPK(nc.vo.bd.psn.PsndocVO.class, uapPsnVO.getPk_psndoc(), false);
  }
  
















  private void doUpdateHRPsndoc(nc.vo.bd.psn.PsndocVO uapPsnVO)
    throws BusinessException
  {
    PsnjobVO[] uapJobVOs = uapPsnVO.getPsnjobs();
    

    String strcondition = "pk_psndoc = '" + uapPsnVO.getPk_psndoc() + "' and " + "lastflag" + " = 'Y' ";
    PsnOrgVO[] orgVOs = (PsnOrgVO[])getServiceTemplate().queryByCondition(PsnOrgVO.class, strcondition);
    

    uapJobVOs = deleteInvalidJobVO(uapJobVOs, orgVOs[0]);
    


    PsndocAggVO psnAggVO = (PsndocAggVO)getServiceTemplate().queryByPk(PsndocAggVO.class, uapPsnVO.getPk_psndoc());
    

    updateHRJobVO(uapJobVOs, orgVOs, psnAggVO);
    

    syncDefineAtrrs(uapPsnVO);
    

    CertVO[] cert = (CertVO[])psnAggVO.getTableVO(CertVO.getDefaultTableName());
    if ((cert != null) && (cert.length > 0))
    {
      cert[0].setIdtype(psnAggVO.getParentVO().getIdtype());
      cert[0].setId(psnAggVO.getParentVO().getId());
      cert[0].setStatus(1);
      
      getServiceTemplate().update(cert[0], false);
    }
    

    PsnJobVO mainJob = psnAggVO.getParentVO().getPsnJobVO();
    
    orgVOs[0].setPk_group(mainJob.getPk_hrgroup());
    orgVOs[0].setPk_org(mainJob.getPk_org());
    orgVOs[0].setPk_hrorg(mainJob.getPk_hrorg());
    orgVOs[0].setBegindate(mainJob.getBegindate());
    
    orgVOs[0].setStatus(1);
    getServiceTemplate().update(orgVOs[0], false);
    psnAggVO.getParentVO().setPsnOrgVO(orgVOs[0]);
    

    PsnChgVO[] psnchg = (PsnChgVO[])psnAggVO.getTableVO(PsnChgVO.getDefaultTableName());
    if ((psnchg != null) && (psnchg.length > 0))
    {
      String pk_newcorp = HiSQLHelper.getPkCorpByPkOrg(mainJob.getPk_org());
      

      psnchg[0].setPk_corp(pk_newcorp);
      psnchg[0].setBegindate(mainJob.getBegindate());
      psnchg[0].setStatus(1);
      
      getServiceTemplate().update(psnchg[0], false);
    }
  }
  










  public void deletePsndoc(nc.vo.bd.psn.PsndocVO vo)
    throws BusinessException
  {
    nc.itf.bd.psn.psndoc.IPsndocService service = (nc.itf.bd.psn.psndoc.IPsndocService)ObjectCreator.newInstance("baseapp", "nc.impl.bd.psn.psndoc.PsndocServiceImpl");
    if (!PubEnv.isModuleStarted(null, "6007"))
    {
      service.deletePsndoc(vo);
    }
    else
    {
      String pk_psndoc = vo.getPk_psndoc();
      
      service.deletePsndoc(vo);
      

      doDeleteHRPsndoc(pk_psndoc);
    }
  }
  
  private void doDeleteHRPsndoc(String pk_psndoc) throws BusinessException
  {
    String condition = "pk_psndoc= '" + pk_psndoc + "'";
    
    PsnJobVO[] jobVOs = (PsnJobVO[])getServiceTemplate().queryByCondition(PsnJobVO.class, condition);
    
    PsnJobVO mainjob = null;
    if ((jobVOs == null) || (jobVOs.length == 0))
    {
      return;
    }
    for (PsnJobVO job : jobVOs)
    {
      if ((job.getIsmainjob() != null) && (job.getIsmainjob().booleanValue()))
      {
        mainjob = job;
      }
    }
    

    HiEventValueObject.fireEvent(mainjob, null, mainjob.getPk_hrorg(), "218971f0-e5dc-408b-9a32-56529dddd4db", "600705");
    
    PsnOrgVO[] orgVOs = (PsnOrgVO[])getServiceTemplate().queryByCondition(PsnOrgVO.class, condition);
    CertVO[] certVOs = (CertVO[])getServiceTemplate().queryByCondition(CertVO.class, condition);
    PsnChgVO[] psnchgVOs = (PsnChgVO[])getServiceTemplate().queryByCondition(PsnChgVO.class, condition);
    

    MDPersistenceService.lookupPersistenceService().deleteBillFromDB(orgVOs);
    
    MDPersistenceService.lookupPersistenceService().deleteBillFromDB(jobVOs);
    
    MDPersistenceService.lookupPersistenceService().deleteBillFromDB(certVOs);
    
    MDPersistenceService.lookupPersistenceService().deleteBillFromDB(psnchgVOs);
    

    HiEventValueObject.fireEvent(mainjob, null, mainjob.getPk_hrorg(), "218971f0-e5dc-408b-9a32-56529dddd4db", "600706");
    HiEventValueObject.fireDataPermChangeEvent("7156d223-4531-4337-b192-492ab40098f1");
  }
  

  public nc.vo.bd.psn.PsndocVO disEnablePsndoc(nc.vo.bd.psn.PsndocVO vo)
    throws BusinessException
  {
    nc.itf.bd.psn.psndoc.IPsndocService service = (nc.itf.bd.psn.psndoc.IPsndocService)ObjectCreator.newInstance("baseapp", "nc.impl.bd.psn.psndoc.PsndocServiceImpl");
    nc.vo.bd.psn.PsndocVO returnVO = service.disEnablePsndoc(vo);
    return returnVO;
  }
  

  public nc.vo.bd.psn.PsndocVO enablePsndoc(nc.vo.bd.psn.PsndocVO vo)
    throws BusinessException
  {
    nc.itf.bd.psn.psndoc.IPsndocService service = (nc.itf.bd.psn.psndoc.IPsndocService)ObjectCreator.newInstance("baseapp", "nc.impl.bd.psn.psndoc.PsndocServiceImpl");
    nc.vo.bd.psn.PsndocVO returnVO = service.enablePsndoc(vo);
    return returnVO;
  }
  
  public nc.vo.bd.psn.PsndocVO transferPsndoc(nc.vo.bd.psn.PsndocVO vo)
    throws BusinessException
  {
    nc.itf.bd.psn.psndoc.IPsndocService service = (nc.itf.bd.psn.psndoc.IPsndocService)ObjectCreator.newInstance("baseapp", "nc.impl.bd.psn.psndoc.PsndocServiceImpl");
    if (!PubEnv.isModuleStarted(null, "6007"))
    {

      return service.transferPsndoc(vo);
    }
    


    nc.vo.bd.psn.PsndocVO uapPsnVO = service.transferPsndoc(vo);
    
    if ((uapPsnVO.getDataoriginflag() != null) && ((uapPsnVO.getDataoriginflag().intValue() == 1) || (uapPsnVO.getDataoriginflag().intValue() == 2)))
    {

      return uapPsnVO;
    }
    


    doUpdateHRPsndoc(uapPsnVO);
    
    return uapPsnVO;
  }
  

  public nc.vo.bd.psn.PsndocVO deletePsnJob(nc.vo.bd.psn.PsndocVO vo, String pk_org)
    throws BusinessException
  {
    nc.itf.bd.psn.psndoc.IPsndocService service = (nc.itf.bd.psn.psndoc.IPsndocService)ObjectCreator.newInstance("baseapp", "nc.impl.bd.psn.psndoc.PsndocServiceImpl");
    nc.vo.bd.psn.PsndocVO returnVO = service.deletePsnJob(vo, pk_org);
    return returnVO;
  }
  















  private PsndocAggVO updateHRJobVO(PsnjobVO[] uapJobVOs, PsnOrgVO[] orgVOs, PsndocAggVO psnAggVO)
    throws DAOException, BusinessException
  {
    List<PsnJobVO> delMainJobVOList = new ArrayList();
    PsnJobVO[] oldMainJobVOs = (PsnJobVO[])psnAggVO.getTableVO(PsnJobVO.getDefaultTableName());
    
    if (!ArrayUtils.isEmpty(oldMainJobVOs))
    {
      for (int i = 0; i < oldMainJobVOs.length; i++)
      {
        if (oldMainJobVOs[i].getPk_psnorg().equals(orgVOs[0].getPk_psnorg()))
        {
          delMainJobVOList.add(oldMainJobVOs[i]);
        }
      }
      if (!delMainJobVOList.isEmpty())
      {
        getBaseDAO().deleteVOArray((SuperVO[])delMainJobVOList.toArray(new PsnJobVO[0]));
      }
    }
    
    List<PartTimeVO> delPartJobVOList = new ArrayList();
    PartTimeVO[] oldParttimeVOs = (PartTimeVO[])psnAggVO.getTableVO(PartTimeVO.getDefaultTableName());
    if (!ArrayUtils.isEmpty(oldParttimeVOs))
    {
      for (int i = 0; i < oldParttimeVOs.length; i++)
      {
        if (oldParttimeVOs[i].getPk_psnorg().equals(orgVOs[0].getPk_psnorg()))
        {
          delPartJobVOList.add(oldParttimeVOs[i]);
        }
      }
      if (!delPartJobVOList.isEmpty())
      {
        getBaseDAO().deleteVOArray((SuperVO[])delPartJobVOList.toArray(new PsnJobVO[0]));
      }
    }
    

    List<PsnJobVO> jobList = syncHRPsnjobs(orgVOs[0], uapJobVOs);
    

    List<PsnJobVO> mainJobList = new ArrayList();
    List<PsnJobVO> parttimeList = new ArrayList();
    PsnJobVO mainJob = new PsnJobVO();
    for (PsnJobVO psnJobVO : jobList)
    {
      if (psnJobVO.getIsmainjob().booleanValue())
      {
        mainJob = psnJobVO;
        mainJobList.add(psnJobVO);
      }
      else
      {
        parttimeList.add(psnJobVO);
      }
    }
    
    psnAggVO.setTableVO(PsnJobVO.getDefaultTableName(), (CircularlyAccessibleValueObject[])mainJobList.toArray(new PsnJobVO[0]));
    PartTimeVO[] partTimeVOs = (PartTimeVO[])nc.vo.om.pub.SuperVOHelper.createSuperVOsFromSuperVOs((SuperVO[])parttimeList.toArray(new PsnJobVO[0]), PartTimeVO.class);
    psnAggVO.setTableVO(PartTimeVO.getDefaultTableName(), partTimeVOs);
    
    psnAggVO.getParentVO().setPsnJobVO(mainJob);
    
    return psnAggVO;
  }
  









  private List<PsnJobVO> syncHRPsnjobs(PsnOrgVO orgVO, PsnjobVO[] uapJobVOs)
    throws BusinessException
  {
    String pk_psnorg = orgVO.getPk_psnorg();
    

    PsnJobVO[] psnJobVOs = syncJobDefine(uapJobVOs);
    
    List<PsnJobVO> jobList = new ArrayList();
    int assgid = 2;
    






    List<Object> list = handleHRPsnjobs(psnJobVOs);
    List<UFLiteralDate> listdates = (List)list.get(0);
    PsnJobVO Maxpsnjobvo = (PsnJobVO)list.get(1);
    PsnJobVO Minpsnjobvo = (PsnJobVO)list.get(2);
    
    if (Maxpsnjobvo.getEnddate() != null)
    {
      PsnJobVO newpsnjobvo = (PsnJobVO)Maxpsnjobvo.clone();
      PsnJobVO psnjobquitvo = getQuitData(newpsnjobvo, assgid, pk_psnorg);
      jobList.add(psnjobquitvo);
      
      orgVO.setEndflag(UFBoolean.TRUE);
      orgVO.setEnddate(Maxpsnjobvo.getEnddate());
    }
    
    for (PsnJobVO psnJobVO : psnJobVOs)
    {
      if (psnJobVO.getBegindate() == null)
      {
        throw new BusinessException(ResHelper.getString("6007psn", "06007psn0300"));
      }
      


      if (psnJobVO.getIsmainjob().booleanValue())
      {
        psnJobVO.setAssgid(Integer.valueOf(1));
      }
      else
      {
        psnJobVO.setAssgid(Integer.valueOf(assgid));
        assgid++;
      }
      
      if (psnJobVO.getPk_job() != null)
      {
        psnJobVO.setSeries(getPKJobType(psnJobVO.getPk_job()));
      }
      
      if (psnJobVO.getPk_post() != null)
      {
        psnJobVO.setPk_postseries(getPKPostSeries(psnJobVO.getPk_post()));
      }
      
      psnJobVO.setPk_hrgroup(psnJobVO.getPk_group());
      
      psnJobVO.setPk_hrorg(getPkHrorg(psnJobVO.getPk_org()));
      psnJobVO.setPk_psnorg(pk_psnorg);
      psnJobVO.setPsntype(Integer.valueOf(0));
      List<PsnJobVO> mainjobs = new ArrayList();
      for (int i = 0; i < psnJobVOs.length; i++)
      {
        PsnJobVO job = psnJobVOs[i];
        if (job.getIsmainjob().booleanValue())
        {
          mainjobs.add(job);
        }
      }
      

      if (psnJobVO.getIsmainjob().booleanValue())
      {
        if (mainjobs.size() == 1)
        {
          psnJobVO.setLastflag(Maxpsnjobvo.getEnddate() == null ? UFBoolean.TRUE : UFBoolean.FALSE);
          psnJobVO.setEndflag(Maxpsnjobvo.getEnddate() == null ? UFBoolean.FALSE : UFBoolean.TRUE);
          psnJobVO.setRecordnum(Maxpsnjobvo.getEnddate() == null ? Integer.valueOf(listdates.size() - 1) : Integer.valueOf(listdates.size()));
          
          psnJobVO.setPoststat(Maxpsnjobvo.getEnddate() == null ? UFBoolean.TRUE : UFBoolean.FALSE);
          psnJobVO.setTrnsevent(Integer.valueOf(1));


        }
        else if (psnJobVO.getBegindate().equals(Minpsnjobvo.getBegindate()))
        {
          psnJobVO.setLastflag(UFBoolean.FALSE);
          psnJobVO.setEndflag(Minpsnjobvo.getEnddate() == null ? UFBoolean.FALSE : UFBoolean.TRUE);
          psnJobVO.setRecordnum(Maxpsnjobvo.getEnddate() == null ? Integer.valueOf(listdates.size() - 1) : Integer.valueOf(listdates.size()));
          
          psnJobVO.setTrnsevent(Integer.valueOf(1));
          psnJobVO.setPoststat(UFBoolean.FALSE);
        }
        else if (psnJobVO.getBegindate().equals(Maxpsnjobvo.getBegindate()))
        {
          psnJobVO.setLastflag(Maxpsnjobvo.getEnddate() == null ? UFBoolean.TRUE : UFBoolean.FALSE);
          psnJobVO.setEndflag(Maxpsnjobvo.getEnddate() == null ? UFBoolean.FALSE : UFBoolean.TRUE);
          psnJobVO.setRecordnum(Maxpsnjobvo.getEnddate() == null ? Integer.valueOf(0) : Integer.valueOf(1));
          psnJobVO.setTrnsevent(Integer.valueOf(3));
          psnJobVO.setPoststat(Maxpsnjobvo.getEnddate() == null ? UFBoolean.TRUE : UFBoolean.FALSE);
        }
        else
        {
          psnJobVO.setLastflag(UFBoolean.FALSE);
          psnJobVO.setEndflag(Maxpsnjobvo.getEnddate() == null ? UFBoolean.FALSE : UFBoolean.TRUE);
          for (int i = 0; i < listdates.size(); i++)
          {
            if (psnJobVO.getBegindate().equals(listdates.get(i)))
            {
              psnJobVO.setRecordnum(Integer.valueOf(listdates.size() - i - 1));
            }
          }
          psnJobVO.setTrnsevent(Integer.valueOf(3));
          psnJobVO.setPoststat(UFBoolean.FALSE);
        }
        

      }
      else
      {
        if (psnJobVO.getEnddate() == null)
        {
          psnJobVO.setLastflag(UFBoolean.TRUE);
          psnJobVO.setEndflag(UFBoolean.FALSE);
          psnJobVO.setTrnsevent(Integer.valueOf(1));
          psnJobVO.setPoststat(UFBoolean.TRUE);
        } else {
          psnJobVO.setLastflag(UFBoolean.FALSE);
          psnJobVO.setEndflag(UFBoolean.TRUE);
          psnJobVO.setTrnsevent(Integer.valueOf(4));
          psnJobVO.setPoststat(UFBoolean.FALSE);
        }
        psnJobVO.setRecordnum(Integer.valueOf(0));
      }
      
      int showorder = psnJobVO.getShoworder() == null ? 9999999 : psnJobVO.getShoworder().intValue();
      psnJobVO.setShoworder(Integer.valueOf(showorder));
      if (StringUtils.isNotBlank(psnJobVO.getClerkcode()))
      {
        psnJobVO.setClerkcode(psnJobVO.getClerkcode().trim());
      }
      

      jobList.add(psnJobVO);
    }
    

    getServiceTemplate().update(orgVO, false);
    
    getBaseDAO().insertVOArrayWithPK((SuperVO[])jobList.toArray(new PsnJobVO[jobList.size()]));
    
    return jobList;
  }
  





  public List<Object> handleHRPsnjobs(PsnJobVO[] psnJobVOs)
    throws BusinessException
  {
    List<Object> list = new ArrayList();
    List<UFLiteralDate> listdates = new ArrayList();
    PsnJobVO Maxpsnjobvo = new PsnJobVO();
    PsnJobVO Minpsnjobvo = new PsnJobVO();
    
    for (int i = 0; i < psnJobVOs.length; i++)
    {
      if (psnJobVOs[i].getIsmainjob().booleanValue())
        listdates.add(psnJobVOs[i].getBegindate());
    }
    Collections.sort(listdates);
    
    for (int j = 0; j < psnJobVOs.length; j++)
    {
      if (psnJobVOs[j].getIsmainjob().booleanValue())
      {
        if (psnJobVOs[j].getBegindate().equals(listdates.get(0)))
        {
          Minpsnjobvo = psnJobVOs[j];
        }
        
        if (psnJobVOs[j].getBegindate().equals(listdates.get(listdates.size() - 1)))
        {
          Maxpsnjobvo = psnJobVOs[j];
        }
      }
    }
    list.add(listdates);
    list.add(Maxpsnjobvo);
    list.add(Minpsnjobvo);
    return list;
  }
  
  public PsnJobVO getQuitData(PsnJobVO Maxpsnjobvo, int assgid, String pk_psnorg)
    throws BusinessException
  {
    if (Maxpsnjobvo.getEnddate() != null)
    {
      if (Maxpsnjobvo.getBegindate() == null)
      {
        throw new BusinessException(ResHelper.getString("6007psn", "06007psn0300"));
      }
      


      if (Maxpsnjobvo.getIsmainjob().booleanValue())
      {
        Maxpsnjobvo.setAssgid(Integer.valueOf(1));
      }
      else
      {
        Maxpsnjobvo.setAssgid(Integer.valueOf(assgid));
        assgid++;
      }
      
      if (Maxpsnjobvo.getPk_job() != null)
      {
        Maxpsnjobvo.setSeries(getPKJobType(Maxpsnjobvo.getPk_job()));
      }
      
      if (Maxpsnjobvo.getPk_post() != null)
      {
        Maxpsnjobvo.setPk_postseries(getPKPostSeries(Maxpsnjobvo.getPk_post()));
      }
      
      Maxpsnjobvo.setBegindate(Maxpsnjobvo.getEnddate().getDateAfter(1));
      Maxpsnjobvo.setEnddate(null);
      Maxpsnjobvo.setPk_hrgroup(Maxpsnjobvo.getPk_group());
      
      Maxpsnjobvo.setPk_hrorg(getPkHrorg(Maxpsnjobvo.getPk_org()));
      Maxpsnjobvo.setPk_psnorg(pk_psnorg);
      Maxpsnjobvo.setPsntype(Integer.valueOf(0));
      Maxpsnjobvo.setLastflag(UFBoolean.TRUE);
      Maxpsnjobvo.setEndflag(UFBoolean.TRUE);
      Maxpsnjobvo.setRecordnum(Integer.valueOf(0));
      Maxpsnjobvo.setTrnsevent(Integer.valueOf(4));
      Maxpsnjobvo.setPoststat(UFBoolean.FALSE);
      Maxpsnjobvo.setPk_psnjob(null);
      int showorder = Maxpsnjobvo.getShoworder() == null ? 9999999 : Maxpsnjobvo.getShoworder().intValue();
      Maxpsnjobvo.setShoworder(Integer.valueOf(showorder));
      if (StringUtils.isNotBlank(Maxpsnjobvo.getClerkcode()))
      {
        Maxpsnjobvo.setClerkcode(Maxpsnjobvo.getClerkcode().trim());
      }
    }
    
    return Maxpsnjobvo;
  }
  







  private PsnOrgVO SyncCreateOrg(PsnjobVO[] uapJobVOs, int orgrelaid)
    throws BusinessException
  {
    PsnOrgVO orgVO = null;
    InSQLCreator isc = new InSQLCreator();
    ArrayList<String> pk_psndoc_List = new ArrayList();
    
    for (PsnjobVO psnjobVO : uapJobVOs)
    {
      if (psnjobVO.getIsmainjob().booleanValue())
      {
        orgVO = (PsnOrgVO)nc.vo.om.pub.SuperVOHelper.createSuperVOFromSuperVO(psnjobVO, PsnOrgVO.class);
        orgVO.setOrgrelaid(Integer.valueOf(orgrelaid));
        orgVO.setPsntype(Integer.valueOf(0));
        orgVO.setBegindate(psnjobVO.getIndutydate());
        orgVO.setIndocflag(UFBoolean.TRUE);
        orgVO.setLastflag(UFBoolean.TRUE);
        orgVO.setEndflag(UFBoolean.FALSE);
        if (orgVO.getPk_hrorg() == null)
        {
          orgVO.setPk_hrorg(getPkHrorg(orgVO.getPk_org()));
        }
        pk_psndoc_List.add(psnjobVO.getPk_psndoc());
      }
    }
    if (pk_psndoc_List.size() > 0)
    {
      String strInSql = isc.getInSQL((String[])pk_psndoc_List.toArray(new String[0]));
      
      String strSQL = "update " + PsnOrgVO.getDefaultTableName() + " set " + "lastflag" + "='" + UFBoolean.FALSE + "' where " + "pk_psndoc" + " in (" + strInSql + ") ";
      


      getBaseDAO().executeUpdate(strSQL);
    }
    orgVO = (PsnOrgVO)getServiceTemplate().insert(orgVO);
    
    return orgVO;
  }
  
  private String getPkHrorg(String pkOrg) throws BusinessException
  {
    OrgVO org = ((IAOSQueryService)NCLocator.getInstance().lookup(IAOSQueryService.class)).queryHROrgByOrgPK(pkOrg);
    if (org == null)
    {
      String orgName = VOUtils.getDocName(OrgVO.class, pkOrg);
      throw new BusinessException(ResHelper.getString("6007psn", "06007psn0301", new String[] { orgName }));
    }
    


    return org.getPk_org();
  }
  
  private String getPKJobType(String pk_job) throws BusinessException
  {
    String pk_jobtype = "";
    JobVO jobVO = (JobVO)getServiceTemplate().queryByPk(JobVO.class, pk_job);
    pk_jobtype = jobVO.getPk_jobtype();
    
    return pk_jobtype;
  }
  
  private String getPKPostSeries(String pk_post) throws BusinessException
  {
    String pk_postseries = "";
    PostVO postVO = (PostVO)getServiceTemplate().queryByPk(PostVO.class, pk_post);
    pk_postseries = postVO.getPk_postseries();
    
    return pk_postseries;
  }
  





  private void syncCertVO(nc.vo.bd.psn.PsndocVO uapPsnVO)
    throws BusinessException
  {
    nc.vo.hi.psndoc.PsndocVO psnVO = (nc.vo.hi.psndoc.PsndocVO)getServiceTemplate().queryByPk(nc.vo.hi.psndoc.PsndocVO.class, uapPsnVO.getPk_psndoc(), true);
    
    if (StringUtils.isNotBlank(psnVO.getCode()))
    {
      psnVO.setCode(psnVO.getCode().trim());
    }
    if (StringUtils.isNotBlank(psnVO.getName()))
    {
      psnVO.setName(psnVO.getName().trim());
    }
    if (StringUtils.isNotBlank(psnVO.getName2()))
    {
      psnVO.setName2(psnVO.getName2().trim());
    }
    if (StringUtils.isNotBlank(psnVO.getName3()))
    {
      psnVO.setName3(psnVO.getName3().trim());
    }
    if (StringUtils.isNotBlank(psnVO.getName4()))
    {
      psnVO.setName4(psnVO.getName4().trim());
    }
    if (StringUtils.isNotBlank(psnVO.getName5()))
    {
      psnVO.setName5(psnVO.getName5().trim());
    }
    if (StringUtils.isNotBlank(psnVO.getName6()))
    {
      psnVO.setName6(psnVO.getName6().trim());
    }
    getServiceTemplate().update(psnVO, false);
    
    CertVO cert = new CertVO();
    cert.setPk_psndoc(psnVO.getPk_psndoc());
    cert.setIdtype(psnVO.getIdtype());
    cert.setId(psnVO.getId());
    cert.setIseffect(UFBoolean.TRUE);
    cert.setIsstart(UFBoolean.TRUE);
    cert.setStatus(2);
    cert.setPk_group(psnVO.getPk_group());
    cert.setPk_org(psnVO.getPk_org());
    getServiceTemplate().insert(cert);
  }
  







  private void syncDefineAtrrs(nc.vo.bd.psn.PsndocVO uapPsnVO)
    throws DAOException, BusinessException
  {
    Collection<DefineMap> c = getBaseDAO().retrieveByClause(DefineMap.class, "uap_tablename = 'bd_psndoc'");
    if ((c != null) && (!c.isEmpty()))
    {
      String[] srcExtraAttrs = new String[c.size()];
      String[] desExtraAttrs = new String[c.size()];
      DefineMap[] defineMaps = (DefineMap[])c.toArray(new DefineMap[0]);
      for (int i = 0; i < defineMaps.length; i++)
      {
        srcExtraAttrs[i] = defineMaps[i].getPropindex();
        desExtraAttrs[i] = defineMaps[i].getHr_fieldname();
      }
      nc.vo.hi.psndoc.PsndocVO docVO = new nc.vo.hi.psndoc.PsndocVO();
      nc.vo.om.pub.SuperVOHelper.copySuperVOAttributes(new nc.vo.bd.psn.PsndocVO[] { uapPsnVO }, new nc.vo.hi.psndoc.PsndocVO[] { docVO }, srcExtraAttrs, desExtraAttrs, false);
      
      getServiceTemplate().update(docVO, false);
    }
  }
  








  private PsnJobVO[] syncJobDefine(PsnjobVO[] uapJobVOs)
    throws DAOException
  {
    Collection<DefineMap> c = getBaseDAO().retrieveByClause(DefineMap.class, "uap_tablename = 'bd_psnjob'");
    String[] srcExtraAttrs = new String[c.size() + 3];
    String[] desExtraAttrs = new String[c.size() + 3];
    srcExtraAttrs[0] = "psncode";
    srcExtraAttrs[1] = "indutydate";
    srcExtraAttrs[2] = "enddutydate";
    desExtraAttrs[0] = "clerkcode";
    desExtraAttrs[1] = "begindate";
    desExtraAttrs[2] = "enddate";
    if ((c != null) && (!c.isEmpty()))
    {
      DefineMap[] defineMaps = (DefineMap[])c.toArray(new DefineMap[0]);
      for (int i = 0; i < defineMaps.length; i++)
      {
        srcExtraAttrs[(i + 3)] = defineMaps[i].getPropindex();
        desExtraAttrs[(i + 3)] = defineMaps[i].getHr_fieldname();
      }
    }
    PsnJobVO[] psnJobVOs = (PsnJobVO[])nc.vo.om.pub.SuperVOHelper.createSuperVOsFromSuperVOs(uapJobVOs, PsnJobVO.class, srcExtraAttrs, desExtraAttrs, false);
    return psnJobVOs;
  }
  
  private SimpleDocServiceTemplate getServiceTemplate()
  {
    if (this.serviceTemplate == null)
    {
      this.serviceTemplate = new SimpleDocServiceTemplate("SyncUAPDataToHRImpl");
    }
    return this.serviceTemplate;
  }
  
  public BaseDAO getBaseDAO()
  {
    if (this.baseDAO == null)
    {
      this.baseDAO = new BaseDAO("NC65");
    }
    return this.baseDAO;
  }
  

  public boolean isHREnabled(String pk_org)
    throws BusinessException
  {
    String busiAction = InvocationInfoProxy.getInstance().getServiceName();
    

    if (busiAction!=null&&busiAction.equalsIgnoreCase(nc.itf.bd.psn.psndoc.IPsndocService.class.getName()))
    {
      if (PubEnv.isModuleStarted(null, "6007"))
      {
        int count = ((IPersistenceRetrieve)NCLocator.getInstance().lookup(IPersistenceRetrieve.class)).getCountByCondition("org_admin_enable", " pk_adminorg ='" + pk_org + "' ");
        

        return count > 0;
      }
    }
    
    return false;
  }
  

  public void transferUser(nc.vo.bd.psn.PsndocVO vo, boolean innerGroup)
    throws BusinessException
  {
    nc.itf.bd.psn.psndoc.IPsndocService service = (nc.itf.bd.psn.psndoc.IPsndocService)ObjectCreator.newInstance("baseapp", "nc.impl.bd.psn.psndoc.PsndocServiceImpl");
    service.transferUser(vo, innerGroup);
  }
  
  public Map<String, UFBoolean> isHREnabled(String[] pk_orgs)
    throws BusinessException
  {
    if (!PubEnv.isModuleStarted(null, "6007"))
    {
      nc.itf.bd.psn.psndoc.IPsndocService service = (nc.itf.bd.psn.psndoc.IPsndocService)ObjectCreator.newInstance("baseapp", "nc.impl.bd.psn.psndoc.PsndocServiceImpl");
      return service.isHREnabled(pk_orgs);
    }
    
    Map<String, UFBoolean> map = new HashMap();
    for (String pk_org : pk_orgs)
    {
      map.put(pk_org, UFBoolean.valueOf(isHREnabled(pk_org)));
    }
    return map;
  }
  

  public PsndocExtend[] createUser(PsndocExtend[] vo, String pk_userGroup, String passwordStrategy)
    throws BusinessException
  {
    nc.itf.bd.psn.psndoc.IPsndocService service = (nc.itf.bd.psn.psndoc.IPsndocService)ObjectCreator.newInstance("baseapp", "nc.impl.bd.psn.psndoc.PsndocServiceImpl");
    PsndocExtend[] vos = service.createUser(vo, pk_userGroup, passwordStrategy);
    return vos;
  }
  
  public void transferUserCrossGroup(nc.vo.bd.psn.PsndocVO vo, boolean ParamValue)
    throws BusinessException
  {
    nc.itf.bd.psn.psndoc.IPsndocService service = (nc.itf.bd.psn.psndoc.IPsndocService)ObjectCreator.newInstance("baseapp", "nc.impl.bd.psn.psndoc.PsndocServiceImpl");
    service.transferUserCrossGroup(vo, ParamValue);
  }
  






  private void doInsertHRPsndocWhenRehire(nc.vo.bd.psn.PsndocVO uapPsnVO, boolean isSyncCert, int orgrelaid)
    throws BusinessException
  {
    PsnjobVO[] uapJobVO = uapPsnVO.getPsnjobs();
    

    uapJobVO = deleteFirstJobVO(uapJobVO);
    

    if ((uapJobVO == null) || (uapJobVO.length == 0))
    {
      return;
    }
    

    syncDefineAtrrs(uapPsnVO);
    
    if (isSyncCert)
    {

      syncCertVO(uapPsnVO);
    }
    


    PsnOrgVO orgVO = SyncCreateOrg(uapJobVO, orgrelaid);
    

    List<PsnJobVO> jobList = syncHRPsnjobs(orgVO, uapJobVO);
    
    PsnJobVO mainJobVO = null;
    
    for (PsnJobVO psnJobVO : jobList)
    {
      if ((psnJobVO.getIsmainjob() != null) && (psnJobVO.getIsmainjob().booleanValue()))
      {


        mainJobVO = psnJobVO;
        ((IPersonRecordService)NCLocator.getInstance().lookup(IPersonRecordService.class)).addPsnChgWhenIntoDoc(psnJobVO);
      }
    }
    

    if (PubEnv.isModuleStarted(PubEnv.getPk_group(), "6011"))
    {

      ConttypeStateVO stateVO = new ConttypeStateVO();
      stateVO.setAssgid(mainJobVO.getAssgid());
      stateVO.setContstate(Integer.valueOf(1));
      stateVO.setConttype("hi_psndoc_ctrt");
      stateVO.setPk_psndoc(uapPsnVO.getPk_psndoc());
      stateVO.setPk_psnorg(orgVO.getPk_psnorg());
      getServiceTemplate().insert(stateVO);
    }
  }
  








  private PsnjobVO[] deleteFirstJobVO(PsnjobVO[] uapJobVOs)
    throws BusinessException
  {
    String pk_psndoc = uapJobVOs[0].getPk_psndoc();
    
    List<PsnjobVO> insertJobs = new ArrayList();
    
    String condition = " pk_psndoc= '" + pk_psndoc + "' ";
    PsnJobVO[] hrJobVOs;
    try {
      hrJobVOs = (PsnJobVO[])getServiceTemplate().queryByCondition(PsnJobVO.class, condition);
    } catch (BusinessException e) {
      throw new BusinessException(e.getMessage());
    }
    
    if ((hrJobVOs != null) && (hrJobVOs.length > 0)) {
      List<String> pk_hrPsnjobs = new ArrayList();
      for (int i = 0; i < hrJobVOs.length; i++) {
        pk_hrPsnjobs.add(hrJobVOs[i].getPk_psnjob());
      }
      for (int i = 0; i < uapJobVOs.length; i++)
      {
        PsnjobVO psnjobVO = uapJobVOs[i];
        String pk_psnjob = psnjobVO.getPk_psnjob();
        if (!pk_hrPsnjobs.contains(pk_psnjob))
        {
          insertJobs.add(psnjobVO);
        }
      }
      return (PsnjobVO[])insertJobs.toArray(new PsnjobVO[0]);
    }
    
    return uapJobVOs;
  }
  






  private PsnjobVO[] deleteInvalidJobVO(PsnjobVO[] uapJobVOs, PsnOrgVO psnOrgVO)
    throws BusinessException
  {
    String pk_psndoc = uapJobVOs[0].getPk_psndoc();
    String condition = " pk_psndoc= '" + pk_psndoc + "' and pk_psnorg != '" + psnOrgVO.getPk_psnorg() + "' ";
    

    List<PsnjobVO> insertJobs = new ArrayList();
    PsnJobVO[] hrJobVOs;
    try
    {
      hrJobVOs = (PsnJobVO[])getServiceTemplate().queryByCondition(PsnJobVO.class, condition);
    } catch (BusinessException e) {
      throw new BusinessException(e.getMessage());
    }
    
    if ((hrJobVOs != null) && (hrJobVOs.length > 0)) {
      List<String> pk_hrPsnjobs = new ArrayList();
      for (int i = 0; i < hrJobVOs.length; i++) {
        pk_hrPsnjobs.add(hrJobVOs[i].getPk_psnjob());
      }
      for (int i = 0; i < uapJobVOs.length; i++)
      {
        PsnjobVO psnjobVO = uapJobVOs[i];
        String pk_psnjob = psnjobVO.getPk_psnjob();
        if (!pk_hrPsnjobs.contains(pk_psnjob))
        {
          insertJobs.add(psnjobVO);
        }
      }
      return (PsnjobVO[])insertJobs.toArray(new PsnjobVO[0]);
    }
    
    return uapJobVOs;
  }
}
